﻿using StudentMgmt.SQLServerDAL;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace StudentMgmt
{
    public partial class frmNewPunishment : Form
    {
        private int colmun = -1;
        public frmNewPunishment()
        {
            InitializeComponent();
            
        }

        private void frmNewPunishment_Load(object sender, EventArgs e)
        {

        }



        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //下拉列表选择处罚等级，选择后设置文本框的内容
            DataTable myTable = new DataTable();
            int index = 0;
            index = int.Parse(punLevelcomBox.Text);
            PunishLevelConfig plc = new PunishLevelConfig();
            int val = plc.QueryPunishLevel(index, out myTable);
            if (val == 1 && index != 0)
            {
                this.punLeveltBox.Text = myTable.Rows[0]["处罚等级"].ToString();
                //MessageBox.Show(this.punLeveltBox.Text);
            }
        }

        private void select_Click(object sender, EventArgs e)
        {
            //学生信息查找
            string sno = this.StuNo.Text.Trim();
            DataTable table = new DataTable();
            Student student = new Student();
            if (sno.Length >= 1)
            {
                int val = student.QueryStudent(sno, out table);
                //MessageBox.Show("val=" + val + "\ttable.Rows=" + myTable.Rows.Count);
                if (val == 1 && table.Rows.Count > 0)
                {
                    this.StuName.Text = table.Rows[0]["学生姓名"].ToString();
                    this.Sex.Text = table.Rows[0]["性别"].ToString();
                    DateTime birthday = (DateTime)table.Rows[0]["出生日期"];
                    this.Birthday.Text = birthday.ToString("yyyy-MM-dd");
                    this.Class.Text = table.Rows[0]["所在班级"].ToString();
                    this.Department.Text = table.Rows[0]["所在院系"].ToString();
                    this.NativePlace.Text = table.Rows[0]["籍贯"].ToString();
                    this.tips.Text = "学生" + this.StuName.Text.Trim() + "的处罚记录如下";
                    this.SetPunishDGV();
                }
                else
                {
                    this.InitView();
                    MessageBox.Show("当前数据库中不存在学号为 \"" + this.StuNo.Text.Trim() + "\" 的学生信息");
                }
            }
            else
            {
                this.InitView();
                MessageBox.Show("请输入学号进行查询");
            }
            
        }

        private void SetPunishDGV()
        {
            Punishment pun = new Punishment();
            DataTable table1 = new DataTable();
            int val = pun.QueryPunishment(this.StuNo.Text, out table1);
            //MessageBox.Show("val="+val+"\ttable.Rows="+table.Rows.Count);
            if (val == 1 && table1.Rows.Count > 0)
            {
                PunishDGV.DataSource = table1;
                PunishDGV.Columns[0].Visible = false;
            }
            else
            {
                PunishDGV.DataSource = null;
            }
        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            //确认添加处罚记录
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Punishment values (");
            builder.Append("'" + this.StuNo.Text + "','P000" + this.punLevelcomBox.Text + "','");
            builder.Append(DateTime.Now.ToString("yyyy-MM-dd") + "','F'" + ",'" + this.DetailtBox.Text.Trim() + "');");
            string sql = builder.ToString();
            
            try
            {
                SQLClass mysqlclass = new SQLClass(sql);
                mysqlclass.MyExecuteNonQuery();
                //punishment.InsertPunishement(objpunishment);
                MessageBox.Show("添加成功");
                this.SetPunishDGV();
            }
            catch (Exception exp)
            {
                MessageBox.Show("异常类型:\t" + exp.Message);
                MessageBox.Show("添加失败");
            }


        }


        

        private void PunishDGV_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    //若行已是选中状态就不再进行设置
                    if (PunishDGV.Rows[e.RowIndex].Selected == false)
                    {
                        PunishDGV.ClearSelection();
                        PunishDGV.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (PunishDGV.SelectedRows.Count == 1)
                    {
                        PunishDGV.CurrentCell = PunishDGV.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        colmun = e.RowIndex;
                    }
                    //弹出操作菜单
                    //MessageBox.Show(e.RowIndex.ToString());
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        

        private void Approval_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("审批");
            if (colmun != -1)
            {
                if (PunishDGV.Rows[colmun].Cells["审批"].Value.ToString().Trim().Equals("未通过"))
                { 
                StringBuilder builder = new StringBuilder();
                builder.Append("update Punishment set Punishment_Enable = 'T'");
                builder.Append("where Punishment_Id ='" + PunishDGV.Rows[colmun].Cells["ID"].Value.ToString().Trim()+"';");
                SQLClass mysql = new SQLClass(builder.ToString());
                mysql.MyExecuteNonQuery();
                this.SetPunishDGV();
                MessageBox.Show("审批成功");
                }
                else
                {
                    MessageBox.Show("该条记录已经经过审批");
                }
            }
            else
            {
                MessageBox.Show("选择行数过多，请重新选择");
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if (colmun != -1)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("delete from Punishment ");
                builder.Append("where Punishment_Id = ");
                builder.Append("'"+PunishDGV.Rows[colmun].Cells["ID"].Value.ToString().Trim()+"';");
                SQLClass sqlclass = new SQLClass(builder.ToString());
                sqlclass.MyExecuteNonQuery();
                this.SetPunishDGV();
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("选择行数过多，请重新选择");
            }
            //MessageBox.Show("删除");
        }

        

        private void InitView()
        {
            StuNo.Text = "";
            StuName.Text = "";
            Birthday.Text = "";
            Sex.Text = "";
            Class.Text = "";
            Department.Text = "";
            this.NativePlace.Text = "";
            this.tips.Text = "处罚记录";
            PunishDGV.DataSource = null;
        }

        private void PunishDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
